//
//  ProfileViewModel.swift
//  Twitter
//
//  Created by Hoa.wang on 2022/4/7.
//

import Foundation

class ProfileViewModel: ObservableObject {
    
    @Published var tweets = [Tweet]()
    @Published var likedTweets = [Tweet]()

    private let service = TweetService()
    private let userService = UserService()
    let user: User
    
    init(user: User) {
        self.user = user
        fetchUserTweets()
        self.fetchLikedTweets()
    }
    
    var actionButtonTItle: String {
        User.current?.hasSameObjectId(as: user) ?? false ? "Edit Profile" : "Follow"
    }
    
    func fetchUserTweets() {
//        guard let uid = user.id else { return }
//        service.fetchTweets(forUid: uid) { tweets in
//            self.tweets = tweets
//            for i in 0 ..< tweets.count {
//                self.tweets[i].user = self.user
//            }
//        }
    }
    
    func fetchLikedTweets() {
//        guard let uid = user.id else { return }
//        service.fetchLikedTweets(forUid: uid) { tweets in
//            DispatchQueue.main.async {
//                self.likedTweets = tweets
//            }
//
//            for i in 0..<tweets.count {
//                let uid = tweets[i].uid
//                self.userService.fetchUser(withUid: uid) { user in
//                    self.likedTweets[i].user = user
//                }
//            }
//        }
    }
    
    func tweets(forFilter filter: TweetFilterViewModel) -> [Tweet] {
        switch filter {
        case .tweets:
            return tweets
        case .replies:
            return tweets
        case .likes:
            return likedTweets
        }
    }
}
